package com.supermap.fabricsdk;

import com.alibaba.fastjson.JSONArray;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.UUID;

/**
 * @author liqs
 * @version 1.0
 * @date 2021/5/28 16:56
 * 使用一个组织下的两个节点进行数据的读写测试
 *
 * peer0 状态数据库为    CouchDB
 * peer1 状态数据库为    LevelDB
 */
public class SmTransanctionTestDifferentStateDB {

    private String channelName = "txchannel";
    private String chaincodeName = "supermapcc";
    private File networkFileForCouchDB = new File("D:\\blockchain-network-config-byfile-peerCouchDB.yaml");
    private File networkFileForLevelDB = new File("D:\\blockchain-network-config-byfile-peerLevelDB.yaml");
    private SmChain smChainForCouchDB = SmChain.getChain(channelName, networkFileForCouchDB);
    private SmChain smChainForLevelDB = SmChain.getChain(channelName, networkFileForLevelDB);

    /**
     * 存值、取值测试
     *
     * */
    @Test
    public void testInvokeAndQuery() throws InterruptedException {

        /**
         *  write for levelDB && read in couchdb
         */
        String key = UUID.randomUUID().toString();
        String value = UUID.randomUUID().toString();
        smChainForLevelDB.getTransaction().invokeByString(
                chaincodeName,
                "PutStringRecordByKey",
                new String[]{key, value}
        );
        Thread.sleep(2000);
        String queryResult = smChainForCouchDB.getTransaction().queryByString(
                chaincodeName,
                "GetRecordByKey",
                new String[]{key}
        );
        Assert.assertEquals(value, queryResult);

        /**
         * write for couchdb && read in levelDB
         */
        String key1 = UUID.randomUUID().toString();
        String value1 = UUID.randomUUID().toString();
        smChainForCouchDB.getTransaction().invokeByString(
                chaincodeName,
                "PutStringRecordByKey",
                new String[]{key1, value1}
        );
        Thread.sleep(2000);
        String queryResult1 = smChainForLevelDB.getTransaction().queryByString(
                chaincodeName,
                "GetRecordByKey",
                new String[]{key1}
        );
        Assert.assertEquals(value1, queryResult1);


    }

}
